// JavaScript Document
var Layers = Array();
var objCreator;
var bWeResizing = false;
var property;
$(function(){
	
	var myLayout; // a var is required because this page utilizes: myLayout.allowOverflow() method
	$(document).ready(function () {
		var contOpts = {
			onresize: function(e)
			{
				ohGodResizeEvent();
			}
		};
		var innerContOpts = {
			north__size: 400,
			north__initClosed: false,
			onshow: function(e)
			{
				ohGodResizeEvent();
			},		
			onresize: function(e)
			{
				ohGodResizeEvent();
			}
		};
		$('#container').layout(contOpts);
		$('#inner-container').layout(innerContOpts);
		$("#accordion").accordion({
			fillSpace:	true,	
			active:		-1
		});
 	});
	$(window).resize(ohGodResizeEvent);
	InitializeCheckBoxes();

	/*  BEGIN POLYGON CREATION CODE */
	$( ".toggle-button" ).button();

	// My custom marker icon for the polygon
	pointIcon = new google.maps.MarkerImage('jqPlugins/jqGmapsPolygon/marker.gif',
		new google.maps.Size(16, 16),
		new google.maps.Point(0,0),
		new google.maps.Point(8, 8)
	);
	
	// Set Polygon Creator options object properties
	PolygonCreatorOptions.icon = pointIcon;
	
	// Triggered when polygon is closed
	PolygonCreatorOptions.closed = function()
	{
		$('#polygon-mode').attr('checked', false);
		$( "#polygonCreationDialog" ).dialog( "open" );
	}
	
	$('#polygon-mode').click(function()
	{
		if($(this).attr('checked'))
			objCreator = new PolygonCreator(map);
		else
			if(objCreator) objCreator = null;
	});
	
	$('#polygonCreationDialog').dialog({
		autoOpen: false,
		height: 400,
		width: 600,
		modal: true,
		buttons: {
			"Save Polygon": function() {
				var rec = {
					name: 			$('input#name', this).val(),
					color:			$('input#color', this).val(),
					description:	$('textarea#description', this).val(),
					polygon:		objCreator.pen.getData()
				};
				console.log(rec);
				$.get("lib/savePolygon.php", rec);
				$(this).dialog('close');
			},
			Cancel: function() {
				$(this).dialog('close');
			}
		},
		close: function() {
			
			objCreator.destroy();
			objCreator = null;
		}
	});
	$( "#polygonCreationDialog #red, #polygonCreationDialog #green, #polygonCreationDialog #blue" ).slider({
		orientation: "horizontal",
		range: "min",
		max: 255,
		value: 127,
		slide: refreshSwatch,
		change: refreshSwatch
	});
	$("#polygonCreationDialog #red").slider("value", 127);
	$("#polygonCreationDialog #green").slider("value", 127);
	$("#polygonCreationDialog #blue").slider("value", 127);

	$('#PropertyWindow').dialog({
		autoOpen: false,
		width: 600,
		height: 600,
		buttons: {
			"Print": function() { 
				alert('yeah right bitch');
				//$(this).dialog("close"); 
			}, 
			"Close": function() { 
				$(this).dialog("close"); 
			}
		}
	});
	$("#property_tabs").tabs();
	$("#PropertyWindow select#rating").change(SavePropertyAuxInfo);
	$("textarea#comments").blur(SavePropertyAuxInfo);
	
	$('.button').button();
	$("exportKMLForOV2").click(function(e)
	{
		
	});
	$("exportKMLForEarth").click(function(e)
	{
	});

});


function InitializeCheckBoxes()
{
	$('.accordion .head').click(function() {
		$(this).next().toggle('slow');
		return false;
	}).next().hide();
	// begin checkbox code
	$.each(['zip', 'city'], function(idx, layertype)
	{
		$("input."+ layertype +"Check").click(function(eventObject){
			if($(this).is(':checked')){
				//loadKML(layertype, $(this).attr('value'));
				$.cookie($(this).attr('id'), 'checked', {expires:365});
			}
			else
			{
				//unloadKML(layertype, $(this).attr('value'));
				$.cookie($(this).attr('id'), null);
			}
			RefreshData();
		});
	});
	$(".layerCheck:checked").each(function(idx, item)
	{
		loadKML($(item).attr('name'), unescape($(item).val()))
	});

	$(".layerCheck").click(function(e)
	{
		var name = $(e.target).attr('name')
		if($(e.target).is(':checked'))
		{
			loadKML($(e.target).attr('name'), unescape($(e.target).val()));
			$.cookie('Layer' + $(e.target).attr('name'), 'checked', {expires:365});
		}
		else
		{
			unloadKML($(e.target).attr('name'));
			$.cookie('Layer' + $(e.target).attr('name'), null);
		}
		
	});
}

function ohGodResizeEvent()
{
	if(!bWeResizing)
	{
		bWeResizing = true;
		google.maps.event.trigger(map, "resize");
		$("#accordion").accordion("resize");
		bWeResizing = false;
	}
}

//functions
function loadKML(name, sURL)
{
//	var kmlLayer = new google.maps.KmlLayer('http://maps.newfields.com/realestate/map/AdairPark.kml');
	var randomnumber = Math.floor(Math.random() * 666);
	Layers[name] = new google.maps.KmlLayer(sURL, {suppressInfoWindows: false});
	Layers[name].setMap(map);
	/*
	google.maps.event.addListener(Layers[name], 'click', function(kmlEvent) {
		var id = kmlEvent.featureData.description;
		GetPropertyInfo(id);
	});
	*/
}

function unloadKML(name)
{
	if(Layers[name]!=null)
		Layers[name].setMap(null);
}

function loadLayerKML(sName)
{
	var randomnumber = Math.floor(Math.random() * 666)
	Layers[sName] = new google.maps.KmlLayer('http://planetsnook.com/map/conversion/query.kml.php?BY=' + sType + '&' + sType + '=' + sKey + '&' + 'f=' + randomnumber, {suppressInfoWindows: true});
	Layers[cmp].setMap(map);
	google.maps.event.addListener(Layers[cmp], 'click', function(kmlEvent) {
		//var id = kmlEvent.featureData.description;
		GetPropertyInfo(kmlEvent.featureData);
	});
}

function AddToRoute(id, address)
{
	$('#dock li#route ul').append('<li class="routeNode" id="' + id + '">' + address + '</li>');
}

function ExportRoute()
{
	$('#dock li#route ul li.routeNode').each(function(index)
	{
		sOut += "&data[]=" + $(this).attr('id');
	});
	window.open("route.csv.php" + sOut);
}

function hexFromRGB(r, g, b) {
	var hex = [
		r.toString( 16 ),
		g.toString( 16 ),
		b.toString( 16 )
	];
	$.each( hex, function( nr, val ) {
		if ( val.length === 1 ) {
			hex[ nr ] = "0" + val;
		}
	});
	return hex.join( "" ).toUpperCase();
}
function refreshSwatch() {
	var red = $( "#red" ).slider( "value" ),
		green = $( "#green" ).slider( "value" ),
		blue = $( "#blue" ).slider( "value" ),
		hex = hexFromRGB( red, green, blue );
	$( "#swatch" ).css( "background-color", "#" + hex );
	$("input#color").val("#" + hex);
}
